﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
using System.Linq;
using Microsoft.Win32;


namespace SH_VBoxVM_Service.Classes.Installer_Class
{
    [RunInstaller(true)]
    public partial class TestInstaller : System.Configuration.Install.Installer
    {

        string MyServiceName = @"VBoxTrinketService";

        public TestInstaller()
        {
            InitializeComponent();


            //Creates a Service from the installutil.exe like a Class in a project.
            ServiceInstaller installer = new ServiceInstaller();
            installer.ServiceName = MyServiceName; //This has to be the exact Name of the Service that has ServiceBase Class
            installer.DisplayName = MyServiceName;
            installer.Description = "Service Gracefully Controls VM's from VirtualBox Manager with VBoxMagane.exe";
            installer.DelayedAutoStart = true;
            //This helps determine how the service will be started.
            //Options: On StartUp, Manual, or Disabled.
            //This should be decided by the Setup.
            installer.StartType = ServiceStartMode.Automatic;
            base.Installers.Add(installer);

            //Creates an Executable that convokes the Service previously installed.
            //Note: In theory, I can create 10 Services, and run them in a single Service Process
            ServiceProcessInstaller installer2 = new ServiceProcessInstaller();
            installer2.Account = ServiceAccount.LocalSystem;
            
            installer2.Password = null;
            installer2.Username = null;
            base.Installers.Add(installer2);
        }


        //Automagically starts the service on install of the service.
        protected override void OnAfterInstall(IDictionary savedState)
        {
            base.OnAfterInstall(savedState);

            ServiceController sc = new ServiceController(MyServiceName);
            sc.Start();
        }



    }
}
